<template>
  <!--查看选题结果-->
  <div class="view-topic">
    <InnerBreadcrumb v-model="indexKey" :path="breadPath" :showBack="true" />

    <div class="edu-page-warp" v-show="indexKey === 'MAIN'">
      <!--查看老师/学生信息 弹框-->
      <TeacherStuDialog :dialogVisible="dialogVisible" :teacherStuForm="teacherStuForm" :viewType="viewType" @teacher-stu-close="teacherStuClose"></TeacherStuDialog>

      <condition-card @reset="reset" @search="searchList" label-width="140px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.graduationYear')">
              <el-date-picker class="search-item" :clearable="false" v-model="queryForm.grade" @change="handleGradeChange" value-format="yyyy" type="year" :placeholder="$t('graduationDesign.pleaseChoose')">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.adminCollege')">
              <el-select v-model="queryForm.collegeId" filterable @change="handleCollegeChange" class="search-item" clearable>
                <el-option v-for="item in source.studentColleges" :key="item.collegeId" :label="item.collegeIdI18n" :value="item.collegeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.major')">
              <el-select v-model="queryForm.subjectId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectOptions" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.mentor')">
              <el-input v-model="queryForm.teacherName" class="search-item" placeholder="请输入指导教师姓名或工号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.jobTitle')">
              <el-select v-model="queryForm.titleName" filterable class="search-item" clearable>
                <el-option v-for="item in source.jobTitleOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectType')">
              <el-select v-model="queryForm.projectType" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectNature')">
              <el-select v-model="queryForm.projectNatureId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectNatureOptions" :key="item.id" :label="item.subjectProperties" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectSource')">
              <el-select v-model="queryForm.projectSourceId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectSourceOptions" :key="item.id" :label="item.subjectSource" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectName')">
              <el-input v-model="queryForm.projectName" class="search-item" :placeholder="$t('graduationDesign.pleaseEnter')"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectCollege')">
              <el-select v-model="queryForm.optCollegeId" filterable @change="handleOptCollegeChange" class="search-item" clearable>
                <el-option v-for="item in source.collegeOptions" :key="item.collegeId" :label="item.collegeIdI18n" :value="item.collegeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.subjectMajor')">
              <el-select v-model="queryForm.optSubjectId" filterable class="search-item" clearable>
                <el-option v-for="item in source.optSubjectOptions" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never">
        <div class="edu-page-title">{{$t('graduationDesign.viewTopicResult')}}</div>
        <div class="edu-opertion-bar">
          <div class="float-left">
            <el-input v-model="queryForm.studentName" :placeholder="$t('graduationDesign.pleaseEnterStuSearch')" style="width:300px" @keyup.enter.native="searchList" clearable>
              <i slot="suffix" class="el-icon-search" style="margin-top:10px;cursor:pointer" @click="searchList"></i>
            </el-input>
          </div>
          <div class="float-right">
            <el-button @click="onExport" v-loading.fullscreen.lock="loading" element-loading-text="Loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, .48)">{{$t('graduationDesign.export')}}</el-button>
          </div>
        </div>
        <edu-table ref="viewTopicResultTable" :url="getList" @selection-change="handleSelectChange">
          <edu-table-column :label="$t('graduationDesign.graduationYear')" prop="grade" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.student')" prop="studentName" min-width="130">
            <template slot-scope="scope">
              <el-button type="text" @click="onDetail(scope.row, 'STU')">{{`${scope.row.studentName}(${scope.row.studentId})`}}</el-button>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.adminCollege')" prop="collegeIdI18n" min-width="130"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.major')" prop="subjectIdI18n" min-width="130"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.direction')" prop="direction" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.mentor')" prop="applicantName" min-width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="onDetail(scope.row, 'TEACHER')">{{`${scope.row.teacherName}(${scope.row.teacherId})`}}</el-button>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.jobTitle')" prop="titleName" min-width="100"></edu-table-column>
          <el-table-column :label="$t('graduationDesign.subjectCollege')" prop="optCollegeIdI18n" min-width="120"></el-table-column>
          <el-table-column :label="$t('graduationDesign.subjectMajor')" prop="optSubjectIdI18n" min-width="120"></el-table-column>
          <el-table-column :label="$t('graduationDesign.subjectDerection')" prop="optDirectIdI18n" min-width="120"></el-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectName')" prop="projectName" min-width="120"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectNature')" prop="projectNature" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectSource')" prop="projectSource" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.subjectType')" prop="projectType" min-width="90"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.operation')" min-width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="onView(scope.row)">{{$t('graduationDesign.view')}}</el-button>
            </template>
          </edu-table-column>
        </edu-table>
      </el-card>
    </div>

    <div v-if="indexKey === 'VIEW'">
      <!--查看-->
      <TopicInfo :actionType="actionType" :basicInfoForm="basicInfoForm" :progressData="progressData"></TopicInfo>
    </div>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/reviewSubject"
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
  name: 'reviewSubjectDesignated',
  data() {
    return {
      api: API,
      getList: '/api/designservice/optResultInfo/queryByPage',
      queryForm: {
        grade: '',
        collegeId: '',
        subjectId: '',
        projectType: '',
        projectNatureId: '',
        projectSourceId: '',
        projectName: '',
        applicantName: '',
        optCollegeId: '',
        optSubjectId: '',
        teaStatus: '',
        activeAuthId: localStorage.getItem("activeAuthId")
      },
      loading: false,
      indexKey: 'MAIN',
      source: {
        studentColleges: [], // 行政学院
        collegeOptions: [], // 学院
        subjectOptions: [], // 专业
        // 课题类型
        subjectTypeOptions: [{
          label: "毕业论文",
          value: "毕业论文",
        }, {
          label: "毕业设计",
          value: "毕业设计",
        }],
        optSubjectOptions: [],
        subjectNatureOptions: [], // 课题性质
        subjectSourceOptions: [], // 课题来源
        // 职称
        jobTitleOptions: [{
          label: "教授",
          value: "教授"
        }, {
          label: "副教授",
          value: "副教授"
        }, {
          label: "研究员",
          value: "研究员"
        }, {
          label: "助教",
          value: "助教"
        }]
      },
      breadPath: {
        label: this.$t('graduationDesign.viewTopicResult'),
        key: "MAIN",
        children: [
          {
            key: "VIEW",
            label: this.$t('graduationDesign.viewSubject')
          }
        ]
      },
      subjectSelectedData: [], // 勾选的列表数据
      actionType: "",
      basicInfoForm: {},
      progressData: [], //审核进度列表
      dialogVisible: false,
      teacherStuForm: {}, // 查看老师/学生信息表单
      viewType: ''
    };
  },
  created() {
    this.queryForm.grade = new Date().getFullYear() + '';
    this.getCollege();
    this.getSubjectSource();
    this.getSubjectNature();
    tools.initAllCollege(this.source.studentColleges);
  },
  mounted() {
    this.searchList();
  },
  methods: {
    searchList() {
      this.$refs.viewTopicResultTable.search(this.queryForm);
    },
    reset() {
      this.queryForm = {
        grade: '',
        collegeId: '',
        subjectId: '',
        projectType: '',
        projectNatureId: '',
        projectSourceId: '',
        projectName: '',
        optCollegeId: '',
        optSubjectId: '',
        applicantName: '',
        teaStatus: '',
        activeAuthId: localStorage.getItem("activeAuthId")
      };
      this.source.subjectOptions = [];
      this.source.optSubjectOptions = [];
      this.queryForm.grade = new Date().getFullYear() + '';
      this.getSubjectSource();
      this.getSubjectNature();
      this.searchList();
    },
    handleSelectChange(val) {
      this.subjectSelectedData = val;
    },
    handleGradeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.collegeId) {
        this.handleCollegeChange(this.queryForm.collegeId);
      }
      if (val && this.queryForm.optCollegeId) {
        this.handleOptCollegeChange(this.queryForm.optCollegeId);
      }
      this.getSubjectSource();
      this.getSubjectNature();
      this.searchList();
    },
    // 查学院
    getCollege() {
      this.api.getCollege().then(res => {
        this.source.collegeOptions = res;
      });
    },
    // 选择毕业年份和学院查专业
    handleCollegeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.grade) {
        const params = {
          grade: this.queryForm.grade,
          college: val
        };
        this.api.getSubject(params).then(res => {
          this.source.subjectOptions = res.data;
        }).catch();
      }
    },
    handleOptCollegeChange(val) {
      this.queryForm.optSubjectId = "";
      this.source.optSubjectOptions = [];
      if (val && this.queryForm.grade) {
        const params = {
          grade: this.queryForm.grade,
          college: val
        };
        this.api.getSubject(params).then(res => {
          this.source.optSubjectOptions = res.data;
        }).catch();
      }
    },
    // 查课题来源
    getSubjectSource() {
      const params = {
        year: this.queryForm.grade
      };
      this.api.getSource(params).then(res => {
        this.source.subjectSourceOptions = res.data;
      }).catch();
    },
    // 查课题性质
    getSubjectNature() {
      const params = {
        year: this.queryForm.grade
      };
      this.api.getNature(params).then(res => {
        this.source.subjectNatureOptions = res.data;
      }).catch();
    },
    // 查审核进度
    getReviewProgress(applyNo) {
      this.api.getProgress(applyNo).then(res => {
        this.progressData = res.data.listApproveHistory;
      }).catch();
    },
    // 查看
    onView(row) {
      this.actionType = "VIEW_TOPIC_RESULT";
      const params = {
        studentId: row.studentId,
        infoId: row.infoId,
        infoType: row.infoType
      };
      this.api.getSubjectDetail(params).then(({ data }) => {
        this.indexKey = 'VIEW';
        this.basicInfoForm = { ...(data.applicationTeacherEntity || data.applicationStudentEntity) };
        this.basicInfoForm.grade = row.grade ? row.grade + '' : '';
        this.basicInfoForm.flowNo = row.flowNo;
        this.basicInfoForm.stuCollegeI18n = data.stuCollegeI18n;
        this.basicInfoForm.stuSubjectIdI18n = data.stuSubjectIdI18n;
        this.basicInfoForm.applicantName = row.teacherName;
        this.basicInfoForm.teacherName = row.teacherName;
        this.basicInfoForm.studentName = row.studentName;
        this.progressData = [];
        if (row.flowNo) {
          this.getReviewProgress(row.flowNo);
        }
      }).catch();
    },
    // 查看老师/学生信息
    onDetail(row, type) {
      this.viewType = type;
      let reqApi = null;
      if (type === "STU") {
        reqApi = this.api.getStuInfo(row.studentId);
      } else {
        reqApi = this.api.getTeacherInfo(row.teacherId);
      }
      reqApi.then(res => {
        this.dialogVisible = true;
        this.teacherStuForm = { ...res.data };
        if (type === "STU") {
          this.teacherStuForm.teacherName = `${row.teacherName}(${row.teacherId})`;
        }
      });
    },
    // 返回
    teacherStuClose() {
      this.dialogVisible = false;
    },
    // 导出
    onExport() {
      const params = Object.assign(this.queryForm, { exprotList: this.subjectSelectedData });
      this.api.resultExport(params).then(res => {
        if (res.code == 200) {
          // this.delayRequest(res.data.key);
          tools.downloadFile(res.data.key, "查看选题结果.xls", null, this);
        }
      });
    },
    // 查文件导出状态，等待导出
    async delayRequest(key) {
      this.loading = true;
      await new Promise(resolve => setTimeout(resolve, 3000));
      this.api.getExportStatus(key).then(res => {
        this.loading = false;
        if (res.code === 200 && res.data.status) {
          let path = 'api/designservice/common/export?path=' + res.data.path;
          window.open(path);
        } else {
          this.delayRequest(key);
        }
      }).catch(err => {
        this.loading = false;
        this.delayRequest(key);
      });
    }
  },
  components: {
    TopicInfo: () => import('../components/topicInfo'),
    TeacherStuDialog: () => import('../components/teacherStuDialog')
  }
}
</script>
<style lang="scss" scoped>
.view-topic {
    .search-item {
        width: 100%;
    }
}
</style>
